//
//  KZTimerView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/9/3.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZTimerView.h"

@implementation KZTimerView
{
    UILabel *_timeLabel;
    UILabel *label1;
    UILabel *label2;
    UILabel *label3;
    UILabel *label4;
    UILabel *label5;
    UILabel *moreLabel;
    
    
    CAShapeLayer *__layer;
    
    NSTimer *_timer;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self= [super initWithFrame:frame]) {
        
        self.__resume = true;
        [self setupUI];
        [self addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
        
        // 注册进入后台通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive) name:@"applicationWillResignActive" object:nil];
        
    }
    return self;
}
- (void)applicationWillResignActive {
    if (self.__resume) {
        self.__resume = false;
        [self click];
    }
}

- (void)setupUI {
    
    __layer = [CAShapeLayer layer];
    __layer.lineWidth = 1;
    __layer.fillColor = UIColor.clearColor.CGColor;
    __layer.strokeColor = UIColor.whiteColor.CGColor;
    [self.layer addSublayer:__layer];
    
    CGFloat r = self.height / 2 * 0.8;
    CGFloat cx = self.width / 2;
    CGFloat cy = self.height / 2;
    CGFloat angle = M_PI / 4;
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(cx - r * sin(angle), cy - r * cos(angle))];
    [path addArcWithCenter:CGPointMake(cx, cy) radius:r startAngle:M_PI * 1.5 - angle endAngle:M_PI * 1.5 + angle clockwise:true];
    [path moveToPoint:CGPointMake(cx + r * sin(angle), cy + r * cos(angle))];
    [path addArcWithCenter:CGPointMake(cx, cy) radius:r startAngle:M_PI * 0.5 - angle endAngle:M_PI * 0.5 + angle clockwise:true];
    __layer.path = path.CGPath;
    
    
    //
    label3 = [self createLabel];
    label3.text = @":";
    [label3 sizeToFit];
    label3.centerY = cy;
    label3.centerX = cx;
    //
    label2 = [self createLabel];
    label2.centerY = cy;
    label2.right = label3.left;
    label2.text = @"0";
    //
    label1 = [self createLabel];
    label1.centerY = cy;
    label1.right = label2.left;
    label1.text = @"0";
    //
    label4 = [self createLabel];
    label4.centerY = cy;
    label4.left = label3.right;
    label4.text = @"0";
    //
    label5 = [self createLabel];
    label5.centerY = cy;
    label5.left = label4.right;
    label5.text = @"0";
    
    moreLabel = [self createLabel];
    moreLabel.hidden = true;
    moreLabel.centerY = cy;
    moreLabel.left = label5.right;
    moreLabel.text = @"+";
    
    
    
//    _timeLabel = [[UILabel alloc] init];
//    _timeLabel.adjustsFontSizeToFitWidth = true;
//    _timeLabel.text = @"00:00";
//    _timeLabel.font = FONT(14);
//    _timeLabel.textAlignment = NSTextAlignmentCenter;
//    _timeLabel.textColor = [UIColor whiteColor];
//    [self addSubview:_timeLabel];
//    [_timeLabel sizeToFit];
//    _timeLabel.center = CGPointMake(cx, cy);
}
- (UILabel *)createLabel {
    UILabel *label = [[UILabel alloc] init];
    label.adjustsFontSizeToFitWidth = true;
    label.text = @"9";
    label.font = FONT(14);
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [self addSubview:label];
    [label sizeToFit];
    return label;
}

- (void)setTime:(NSInteger)time {
    
    _time = time;
    
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
- (void)timerAction {
    
    _time ++;
    if (_time >= 3600) {
        moreLabel.hidden = false;
        _time = 3600 - 1;
    }
    NSString *text = [NSString stringWithFormat:@"%02i:%02i", (int)_time/60, (int)_time%60];
    label1.text = [text substringWithRange:NSMakeRange(0, 1)];
    label2.text = [text substringWithRange:NSMakeRange(1, 1)];
    label3.text = [text substringWithRange:NSMakeRange(2, 1)];
    label4.text = [text substringWithRange:NSMakeRange(3, 1)];
    label5.text = [text substringWithRange:NSMakeRange(4, 1)];
}

- (void)click {
    
    self.selected = !self.selected;
    
    // 暂停
    if (self.selected) {
        [self pause];
    }
    
    if (self.clickBlock) {
        self.clickBlock(self.selected);
    }
}

// 暂停
- (void)pause {
    
    [_timer setFireDate:[NSDate distantFuture]];
    
    self.__resume = false;
    self.selected = true;
}
// 恢复
- (void)resume {
    
    [_timer setFireDate:[NSDate date]];
    
    self.__resume = true;
    [self click];
}
- (void)start {
    
    // 重新计时
    self.time = 0;
    // 开启交互
    self.userInteractionEnabled = true;
}
- (void)stop {
    
    // 停止先暂停
    [self pause];
    
    // 关闭定时器
    [_timer invalidate];
    _timer = nil;
    // 关闭交互
    self.userInteractionEnabled = false;
}



@end
